import logging
import unittest
import requests
import utils
from api.login import LoginApi
from api.recharge import RechargeApi


class TestWherherifLogin(unittest.TestCase):

    def setUp(self) -> None:
        self.session = requests.session()
        self.login_api = LoginApi(self.session)
        self.recharge_api = RechargeApi(self.session)

    def tearDown(self) -> None:
        self.session.close()

    # 获取充值验证码成功（r为1）
    def test01_get_recharge_code_success(self):
        # 调用接口
        res_get_recharge_code = self.recharge_api.get_recharge_code(1)

        # 断言
        self.assertEqual(200, res_get_recharge_code.status_code)

        # 输出日志
        logging.info("获取充值验证码的结果为：{}".format(res_get_recharge_code.text))

    # 获取充值成功
    def test02_get_recharge_success(self):

        # 输入数据
        keywords = "13203003591"
        password = "hyc123456"

        # 调用接口
        res_login = self.login_api.login(keywords, password)
        res_get_recharge_code = self.recharge_api.get_recharge_code(1)
        res_get_recharge = self.recharge_api.get_recharge("chinapnrTrust", 70000, "reForm", 8888)

        # 提取数据
        html = res_get_recharge.json()["description"].get("form")
        print(html)
        action, data_form = utils.parse_html(html)
        res_get_recharge_third = self.session.post(url=action,data=data_form)


        # 断言
        utils.common_assert(self, res_login, 200, 200, "登录成功")
        self.assertEqual(200, res_get_recharge_code.status_code)
        utils.common_assert(self,res_get_recharge,200,200,None)
        self.assertEqual(200,res_get_recharge_third.status_code)
        self.assertIn("OK",res_get_recharge_third.text)


        # 输出日志
        logging.info(f"登录结果为：{res_login.json()}")
        logging.info("获取充值验证码的结果为：{}".format(res_get_recharge_code.text))
        logging.info("获取充值的结果为：{}".format(res_get_recharge.json()))
        logging.info("获取充值的结果为：{}".format(res_get_recharge_third.text))
